//
//  ViewController.m
//  CycleOfCollection
//
//  Created by dllo on 16/8/4.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import "CycleLayout.h"
#import "CycleCollectionViewCell.h"
#define kCollectionViewCellReuse @"reuse"
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) NSMutableArray *arrOfImage;
@property (nonatomic, assign) CGFloat maxWidth;
@property (nonatomic, strong) UICollectionView *collection;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.arrOfImage = [NSMutableArray array];
    _maxWidth = 0.0f;
    [self createCollection];
    [self handleData];
     [_collection scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:25] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
}
- (void)handleData {
    for (int i = 0; i < 5; i++) {
        NSString *name = [NSString stringWithFormat:@"hzw%d", i];
        UIImage *imageData = [UIImage imageNamed:name];
        [_arrOfImage addObject:imageData];
    }
}
- (void)createCollection {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(CGRectGetWidth(self.view.bounds), 300);
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 400) collectionViewLayout:layout];
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumLineSpacing =  0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collection.delegate = self;
    _collection.dataSource = self;
    [_collection registerClass:[CycleCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCellReuse];
    _collection.pagingEnabled = YES;
    _collection.bounces = NO;
    _collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection];
    _maxWidth = (_arrOfImage.count - 1) * layout.itemSize.width;
    
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 50;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arrOfImage.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CycleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellReuse forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1];
    cell.imageView.image = _arrOfImage[indexPath.row];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
